package com.tzbank.fingerPrint.sys;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.tzbank.fingerPrint.dao.UtilsMapper;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.SpringContextUtils;
import com.tzbank.fingerPrint.entity.Config;

/**
 * 启动初始化类
 *
 */
@Component
public class ApplicationRunner implements CommandLineRunner,ApplicationContextAware {

	@Autowired
	private UtilsMapper utilsMapper;
	
	
	@Override
	public void run(String... args) throws Exception {
		init();
	}

	/**
	 * 配置信息出初始化 放入redis 缓存
	 */
	private void init() {

		List<Config> fingerPrintConfig = utilsMapper.selectConfig();
		for (Config configvalue : fingerPrintConfig) {
			if ("SERVERURL".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_SERVERURL", configvalue.getConfigValue());
			}
			if ("APP_ID".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_APP_ID", configvalue.getConfigValue());
			}
			if ("RSAPRIVATEKEY".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_RSAPRIVATEKEY", configvalue.getConfigValue());
			}
			if ("FORMAT".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_FORMAT", configvalue.getConfigValue());
			}
			if ("ALIPAYPUBLICKEY".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_ALIPAYPUBLICKEY", configvalue.getConfigValue());
			}
			if ("CHARSET".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_CHARSET", configvalue.getConfigValue());
			}
			if ("SIGNTYPE".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("FINGERPRINT_SIGNTYPE", configvalue.getConfigValue());
			}
			
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtils.setApplicationContext(applicationContext);
	}
}
